'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class CopyFiles

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal directiveStatus As IStatusDirective)
         Dim statusoutput As IStatusOutput
         Dim copyFiles As CodeGenerationSupport.ICopyFileDirective
         Dim targetFileName As String
         Dim sourceFileName As String
         copyFiles = CType(node, CodeGenerationSupport.ICopyFileDirective)
         targetFileName = copyFiles.TargetFileName
         sourceFileName = copyFiles.SourceFileName
         statusoutput = directiveStatus.AddStatusOutput( _
                  targetFileName, _
                  FileTreatment.Unknown)
         Try
            If IO.File.Exists(sourceFileName) Then
               IO.File.Copy(sourceFileName, targetFileName, True)
            ElseIf IO.Directory.Exists(sourceFileName) Then
               Dim dirTarget As IO.DirectoryInfo = IO.Directory.CreateDirectory(targetFileName)
               Dim dirSource As New IO.DirectoryInfo(sourceFileName)
               CopyContainedFiles(dirSource, dirTarget)
            Else
               Throw New System.Exception("File " & sourceFileName & " Not Found")
            End If
            directiveStatus.CountCreated = 1
            directiveStatus.CountSelected = 1
            directiveStatus.CountOutput = 1
         Catch ex As System.Exception
            statusoutput.AddStatusException("CreateMetadata.Run", "Failure creating file", ex)
         End Try
         statusoutput.Status = statusoutput.ChildStatus
      End Sub

      Private Sub CopyContainedFiles(ByVal sourceDir As IO.DirectoryInfo, ByVal targetDir As IO.DirectoryInfo)
         Dim dirTarget As IO.DirectoryInfo
         Dim dirSource As IO.DirectoryInfo
         For Each f As IO.FileInfo In sourceDir.GetFiles
            f.CopyTo(IO.Path.Combine(targetDir.FullName, f.Name))
         Next
         For Each d As IO.DirectoryInfo In sourceDir.GetDirectories
            dirTarget = IO.Directory.CreateDirectory(IO.Path.Combine(targetDir.FullName, d.Name))
            dirSource = New IO.DirectoryInfo(d.FullName)
            CopyContainedFiles(dirSource, dirTarget)
         Next

      End Sub

#End Region
   End Class
End Namespace